package server

import (
	"context"
	"user-server/src/biz"
	"user-server/src/common/constants"
	"user-server/src/model"
	"user-server/src/repo"
	"user-server/src/util/logs"
	pb "user-server/user"
)

type UserServer struct {
	pb.UnimplementedUserServiceServer
}

func (us *UserServer) Login(ctx context.Context, loginRequest *pb.LoginRequest) (*pb.LoginResponse, error) {
	username := loginRequest.Username
	passWd := loginRequest.Passwd

	var userInfo *model.UserInfo

	userCacheService := biz.RefUserCacheService()
	userInfo = userCacheService.GetUserInfoByUserName(ctx, username)
	if userInfo == nil {
		logs.Info("==>Login, Failed to get user from cache ,use db ,username: ", username)

		userInfoDao := repo.RefUserInfoDao()
		user, err := userInfoDao.QueryUserByUsername(username)
		if err != nil {
			logs.Warn("Login, QueryUserByUsername error , username: ", username)
		}

		userInfo = user
		//用户存在-加入缓存
		if userInfo != nil {
			userCacheService.CacheUserInfoByUserName(ctx, userInfo)
		}
	}

	//用户不存在
	if userInfo == nil {
		logs.Info("==>Login, username is not exist , username: %s", username)
		return &pb.LoginResponse{
			Code: constants.USER_WRONG_PASSWORD_ERROR,
			Msg:  constants.CodeMsg[constants.USER_WRONG_PASSWORD_ERROR],
		}, nil
	}

	//密码错误
	if passWd != userInfo.Password {
		logs.Info("==>Login, passwd error , username:", username)
		return &pb.LoginResponse{
			Code: constants.USER_WRONG_PASSWORD_ERROR,
			Msg:  constants.CodeMsg[constants.USER_WRONG_PASSWORD_ERROR],
		}, nil
	}

	return &pb.LoginResponse{
		Code: constants.SUCCESS,
		Msg:  constants.CodeMsg[constants.SUCCESS]}, nil

}

func (us *UserServer) GetUserInfo(ctx context.Context, commonRequest *pb.CommonRequest) (*pb.UserInfoResponse, error) {
	username := commonRequest.Username

	//查询已存在的用户
	var userInfo *model.UserInfo

	userCacheService := biz.RefUserCacheService()
	userInfo = userCacheService.GetUserInfoByUserName(ctx, username)
	if userInfo == nil {
		logs.Info("GetUserInfo, Failed to get user from cache ,use db ,username: ", username)

		userInfoDao := repo.RefUserInfoDao()
		user, err := userInfoDao.QueryUserByUsername(username)
		if err != nil {
			logs.Warn("GetUserInfo, QueryUserByUsername error , username: ", username)
		}
		userInfo = user
		userCacheService.CacheUserInfoByUserName(ctx, userInfo)
	}

	return &pb.UserInfoResponse{
		Username: userInfo.Username,
		Nickname: userInfo.Nickname,
		Profile:  userInfo.Profile,
		Code:     constants.SUCCESS,
		Msg:      constants.CodeMsg[constants.SUCCESS],
	}, nil
}

func (us *UserServer) Edit(ctx context.Context, editRequest *pb.EditRequest) (*pb.EditResponse, error) {
	username := editRequest.Username

	userInfoDao := repo.RefUserInfoDao()
	success, err := userInfoDao.UpdateByUsername(username, editRequest.Nickname, editRequest.Profile)

	if success && err == nil {
		userCacheService := biz.RefUserCacheService()
		userCacheService.DelUserInfoByUserName(ctx, username)

		return &pb.EditResponse{
			Code: constants.SUCCESS,
			Msg:  constants.CodeMsg[constants.SUCCESS],
		}, nil
	}

	logs.Error("UpdateUserInfo, error , username: ", username, err)
	return &pb.EditResponse{
		Code: constants.SYSTEM_ERROR,
		Msg:  constants.CodeMsg[constants.SYSTEM_ERROR],
	}, nil
}
